package com.hooware.controller;

import com.hooware.domain.Product;
import com.hooware.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    /**
     * 查询所有产品并保存到request域
     * @param model
     * @return
     */
    @RequestMapping("/findAll.do")
    public String findAll(Model model) {
        List<Product> productList = productService.findAll();
        model.addAttribute("productList", productList);
        System.out.println("页面访问所有产品: Access ProductController ===> findAll.do --> " + productList);
        return "product-list";
    }

    @RequestMapping("/save.do")
    public String saveProduct(Product product) {
//        modelAndView = new ModelAndView("product-list");
        productService.saveProduct(product);
        System.out.println("页面保存产品: Access ProductController ===> saveProduct.do");
        return "redirect:findAll.do";
        //return findAll(model);
//        return modelAndView;
    }
}
